import sys
import os
import enrol

def print_title():
    """Print program title"""
    print("**************************")
    print("     Enrolment System     ")
    print("**************************")

def print_menu():
    print("q -> Quit")
    print("c -> Clear Screen")
    print("sub -> enrol.subjects()")
    print("subname -> enrol.subjectName(name)")
    print("cs -> enrol.classes(subcode)")
    print("ci -> enrol.classInfo(classID)")
    print("st -> enrol.checkStudent(studentID, [subCode])")
    print("en -> enrol.enrol(studentID, classID)")

def clear_screen():
    os.system(['clear','cls'][os.name == 'nt'])
    print_title()
    print_menu()

en = enrol.Enrol(".\data\\")
raw_input("System initialized press any key to continue...\n")
clear_screen()
keepRunning = True
while(keepRunning):
    command = raw_input("Command: ")
    arg = command.split(" ")
    command = arg[0]
    if command == "q":
        keepRunning = False
    elif command == "c":
        clear_screen()
    elif command == "sub":
        clear_screen()
        print "All course codes:"
        for key in en.subjects():
            print key
    elif command == "subname":
        clear_screen()
        if len(arg) < 2:
            subCode = None
        else:
            subCode = arg[1]
        print "Course Code->[%s] Course Name->[%s]"%(subCode, en.subjectName(subCode))
    elif command == "cs":
        clear_screen()
        if len(arg) < 2:
            print "Please specify Subject Code!"
        else:
            try:
                subCode = arg[1]
                ret = en.classes(subCode)
                print "Course Code->[%s]:"%subCode
                print ret
            except KeyError:
                print "Course Code->[%s] not found"%subCode
    elif command == "ci":
        clear_screen()
        if len(arg) < 2:
            print "Please specify Class ID!"
        else:
            try:
                classID = arg[1]
                ret = en.classInfo(classID)
                print "Class ID->[%s]:"%classID
                print ret
            except KeyError:
                print "Class ID->[%s] not found"%classID
    elif command == "st":
        clear_screen()
        if len(arg) < 2:
            print "Usage:st StudentID [SubjectCode]"
        elif len(arg) == 2:
            print en.checkStudent(arg[1])
        else:
            print en.checkStudent(arg[1], arg[2])
    elif command == "en":
        clear_screen()
        if len(arg) < 2:
            print "Usage:en StudentID ClassID"
        else:
            try:
                ret = en.enrol(arg[1], arg[2])
                print ret
            except KeyError:
                print "Class [%s] not found!"%arg[2]
    else:
        print("Unknown command [%s]") %command

n = raw_input("Press any key to exit...")
